﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Identity.Client;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels.NurseManage;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.NurseManage;
namespace VOL.SmartMecial.Services.NurseManage
{
    public class OtherPorterWorkDetail : IOtherPorterWorkDetail
    {
        private readonly IBaseRepository<Entity.DomainModels.PorterWorkDetail> _porterWorkDetail;
        private readonly IBaseRepository<Entity.DomainModels.WardArea> _wardRep;
        private readonly IBaseRepository<Entity.DomainModels.PorterWardArea>_pWRep;
        private readonly IBaseRepository<Entity.DomainModels.Porter> _porterRep;

        public OtherPorterWorkDetail(IBaseRepository<Entity.DomainModels.PorterWorkDetail> rorterWorkDetailep,  IBaseRepository<Entity.DomainModels.WardArea> wardRep, IBaseRepository<Entity.DomainModels.PorterWardArea> pWRep, IBaseRepository<Entity.DomainModels.Porter> porterRep)
        {
            _porterWorkDetail = rorterWorkDetailep;
            this._wardRep = wardRep;
            _pWRep = pWRep;
            _porterRep = porterRep;
        }

        public async Task<int> AddPorterWorkDetail(Entity.DomainModels.PorterWorkDetail porterWorkDetail)
        {
            porterWorkDetail.WorkTime = DateTime.Now;
            var result = await _porterWorkDetail.AddAsync(porterWorkDetail);
            return result;
        }

        public async Task<ApiPaging<PorterWorkDetailDTO>> ShowPorterWorkDetail(PageDTO pageDto)
        {
            var res=new ApiPaging<PorterWorkDetailDTO>();
            var list = from a in _porterWorkDetail.GetAll()
                       join b in _wardRep.GetAll() on a.WardAreaId equals b.WardAreaCode
                       join c in _pWRep.GetAll() on b.Id equals c.WardAreaId
                       join d in _porterRep.GetAll() on c.PorterId equals d.Id
                       select new PorterWorkDetailDTO
                       {
                           Id = a.Id,
                           PorterId = a.PorterId,
                           WardAreaId = a.WardAreaId,
                           WardId = a.WardId,
                           BedId = a.BedId,
                           ReplaceBedSheet = a.ReplaceBedSheet,
                           ReplaceBedding = a.ReplaceBedding,
                           Disinfect = a.Disinfect,
                           Emit = a.Emit,
                           ReplaceMattress = a.ReplaceMattress,
                           OutDisinfect = a.OutDisinfect,
                           WorkTime = a.WorkTime,
                           PId = a.Id,
                           PorterName = d.PorterName,
                       };
            res.TotalCount = list.Count();
            res.TotalPage = (int)Math.Ceiling(res.TotalCount * 1.0 / pageDto.pageSize);
            res.Items = list.OrderBy(x => x.Id).Skip((pageDto.pageIndex - 1) * pageDto.pageSize).Take(pageDto.pageSize).ToList();
            return await Task.FromResult(res);
             
           
        }
    }
}
